<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      title="物料批次信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          label="批次号"
          :label-width="formLabelWidth"
          prop="batchNum"
          :error="errorMsg.batchNum"
        >
          <el-input
          v-model="formData.batchNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="入库单号"
          :label-width="formLabelWidth"
          prop="orderNum"
          :error="errorMsg.orderNum"
        >
          <el-input
          v-model="formData.orderNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="产品id"
          :label-width="formLabelWidth"
          prop="productId"
          :error="errorMsg.productId"
        >
          <el-input
          v-model="formData.productId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="产品编号"
          :label-width="formLabelWidth"
          prop="productNum"
          :error="errorMsg.productNum"
        >
          <el-input
          v-model="formData.productNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="仓库id"
          :label-width="formLabelWidth"
          prop="storageId"
          :error="errorMsg.storageId"
        >
          <el-input
          v-model="formData.storageId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="仓库Name"
          :label-width="formLabelWidth"
          prop="storageName"
          :error="errorMsg.storageName"
        >
          <el-input
          v-model="formData.storageName"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="证书编号"
          :label-width="formLabelWidth"
          prop="certificateNum"
          :error="errorMsg.certificateNum"
        >
          <el-input
          v-model="formData.certificateNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="供应商id"
          :label-width="formLabelWidth"
          prop="supplierId"
          :error="errorMsg.supplierId"
        >
          <el-input
          v-model="formData.supplierId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="供应商Name"
          :label-width="formLabelWidth"
          prop="supplierName"
          :error="errorMsg.supplierName"
        >
          <el-input
          v-model="formData.supplierName"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="生产日期dateTime"
          :label-width="formLabelWidth"
          prop="dateManufactureTime"
          :error="errorMsg.dateManufactureTime"
        >
          <el-input
          v-model="formData.dateManufactureTime"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="生产日期string"
          :label-width="formLabelWidth"
          prop="dateManufacture"
          :error="errorMsg.dateManufacture"
        >
          <el-input
          v-model="formData.dateManufacture"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="过期时间"
          :label-width="formLabelWidth"
          prop="expirationTime"
          :error="errorMsg.expirationTime"
        >
          <el-input
          v-model="formData.expirationTime"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="初始库存"
          :label-width="formLabelWidth"
          prop="initStock"
          :error="errorMsg.initStock"
        >
          <el-input
          v-model="formData.initStock"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="剩余库存"
          :label-width="formLabelWidth"
          prop="surplusStock"
          :error="errorMsg.surplusStock"
        >
          <el-input
          v-model="formData.surplusStock"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="批次生成时间"
          :label-width="formLabelWidth"
          prop="batchCreateTime"
          :error="errorMsg.batchCreateTime"
        >
          <el-input
          v-model="formData.batchCreateTime"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="批次消耗时间"
          :label-width="formLabelWidth"
          prop="batchConsumeTime"
          :error="errorMsg.batchConsumeTime"
        >
          <el-input
          v-model="formData.batchConsumeTime"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="在库天数"
          :label-width="formLabelWidth"
          prop="daysInStock"
          :error="errorMsg.daysInStock"
        >
          <el-input
          v-model="formData.daysInStock"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="创建人id"
          :label-width="formLabelWidth"
          prop="creatorId"
          :error="errorMsg.creatorId"
        >
          <el-input
          v-model="formData.creatorId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="创建人Name"
          :label-width="formLabelWidth"
          prop="creatorName"
          :error="errorMsg.creatorName"
        >
          <el-input
          v-model="formData.creatorName"
          autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Material/MaterialProductBatch.js";

export default {
  name: "MaterialProductBatchEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        batchNum: "",
        orderNum: "",
        productId: 0,
        productNum: "",
        storageId: 0,
        storageName: "",
        certificateNum: "",
        supplierId: 0,
        supplierName: "",
        dateManufactureTime: 0,
        dateManufacture: "",
        expirationTime: 0,
        initStock: 0,
        surplusStock: 0,
        batchCreateTime: 0,
        batchConsumeTime: 0,
        daysInStock: 0,
        creatorId: 0,
        creatorName: "",
      },
      formData: {
        id: 0,
        batchNum: "",
        orderNum: "",
        productId: 0,
        productNum: "",
        storageId: 0,
        storageName: "",
        certificateNum: "",
        supplierId: 0,
        supplierName: "",
        dateManufactureTime: 0,
        dateManufacture: "",
        expirationTime: 0,
        initStock: 0,
        surplusStock: 0,
        batchCreateTime: 0,
        batchConsumeTime: 0,
        daysInStock: 0,
        creatorId: 0,
        creatorName: "",
      },
      formDataRules: {
        batchNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        orderNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        productId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        productNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        storageId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        certificateNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        supplierId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        supplierName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        dateManufactureTime: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        dateManufacture: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        expirationTime: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        initStock: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        surplusStock: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        batchCreateTime: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        batchConsumeTime: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        daysInStock: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        creatorId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        creatorName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
      },
      initErrorMsg: {
        batchNum: "",
        orderNum: "",
        productId: "",
        productNum: "",
        storageId: "",
        storageName: "",
        certificateNum: "",
        supplierId: "",
        supplierName: "",
        dateManufactureTime: "",
        dateManufacture: "",
        expirationTime: "",
        initStock: "",
        surplusStock: "",
        batchCreateTime: "",
        batchConsumeTime: "",
        daysInStock: "",
        creatorId: "",
        creatorName: "",
      },
      errorMsg: {
        batchNum: "",
        orderNum: "",
        productId: "",
        productNum: "",
        storageId: "",
        storageName: "",
        certificateNum: "",
        supplierId: "",
        supplierName: "",
        dateManufactureTime: "",
        dateManufacture: "",
        expirationTime: "",
        initStock: "",
        surplusStock: "",
        batchCreateTime: "",
        batchConsumeTime: "",
        daysInStock: "",
        creatorId: "",
        creatorName: "",
      },
    };
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("batchNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("orderNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("certificateNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("supplierId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("supplierName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("dateManufactureTime", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("dateManufacture", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("expirationTime", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("initStock", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("surplusStock", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("batchCreateTime", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("batchConsumeTime", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("daysInStock", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("creatorId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("creatorName", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
